package com.wn.sk.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.common.api.Result;
import com.wn.sk.feign.dto.OrderDto;
import com.wn.sk.feign.dto.OrderPayDto;
import com.wn.sk.feign.vo.OrderVo;
import com.wn.sk.order.entity.Order;
import com.wn.sk.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author yellow
 * @since 2022-11-10
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    //创建一个订单
    @ApiOperation("创建一个订单")
    @PostMapping
    public Result<String> createOrder(@RequestBody OrderDto orderDto){

        //创建订单的业务
        String orderSn = orderService.createOrder(orderDto);
        return Result.ok(orderSn);

    }

    //orderSn 通过orderId查询order
    @ApiOperation("通过oderSn查询订单")
    @GetMapping("{orderSn}")
    public Result<OrderVo> findOrderByOrderSn(@PathVariable("orderSn") String  orderSn){

        LambdaQueryWrapper<Order> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Order::getOrderSn,orderSn);

        Order order = orderService.getOne(lambdaQueryWrapper);

        OrderVo orderVo = new OrderVo();
        //order 转成  OrderVo
        //把 order 转成 orderVo (属性的copy)
        BeanUtils.copyProperties(order,orderVo);

        return Result.ok(orderVo);
    }

    @ApiOperation("订单状态的修改")
    @PutMapping("update")
    public Result<String> updateOrder(@RequestBody OrderPayDto orderPayDto){

        //订单状态的修改
        int result = orderService.updateOrder(orderPayDto);

        return result == 1 ? Result.ok("success") : Result.fail("error");
    }

}

